/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.mpd.musicjukebox.ui.components;

import com.bff.bjj.ui.component.MusicListCellRenderer;
import com.bff.mpd.musicjukebox.lib.ColorProperties;
import java.awt.Component;
import java.util.HashMap;
import javax.swing.JList;
import org.bff.javampd.objects.MPDArtist;

/**
 *
 * @author bfindeisen
 */
public class ArtistListCellRenderer extends MusicListCellRenderer {

    private HashMap<MPDArtist, ArtistCellPanel> artistPanels;

    public ArtistListCellRenderer() {
        super();
        setOpaque(true);
        artistPanels = new HashMap<MPDArtist, ArtistCellPanel>();
    }

    @Override
    public Component getListCellRendererComponent(JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean isFocused) {

        ArtistCellPanel panel = null;

        if (value instanceof MPDArtist) {
            MPDArtist artist = (MPDArtist) value;
            panel = artistPanels.get(artist);

            if (panel == null) {
                panel = new ArtistCellPanel(artist, list);
                artistPanels.put(artist, panel);
            }
        } else {
            return super.getListCellRendererComponent(list, value, index, isSelected, isFocused);
        }

        if (panel != null) {
            if ((index % 2) == 0) {
                panel.setEven();
            } else {
                panel.setOdd();
            }

            if (!isSelected) {
                panel.setForeground(ColorProperties.LIST_FOREGROUND);
                panel.setNotSelected();
            } else {
                panel.setSelected();
            }
        }
        
        return panel;
    }
}
